package com.ybu.oodd.controller;

import com.ybu.oodd.pojo.AIParams;
import com.ybu.oodd.pojo.AIResult;
import com.ybu.oodd.pojo.PageResult;
import com.ybu.oodd.pojo.Result;
import com.ybu.oodd.service.AIService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
@RequestMapping("/ai")
@Slf4j
public class AIController {

    @Autowired
    private AIService aiService;

    @Value("${file.upload-dir}")
    private String uploadDir;

    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("文件为空");
        }

        try {
            // 确保目录存在
            Path dir = Paths.get(uploadDir);
            if (!Files.exists(dir)) {
                Files.createDirectories(dir);
            }

            // 获取扩展名
            String originalFilename = file.getOriginalFilename();
            String ext = "";
            if (originalFilename != null && originalFilename.contains(".")) {
                ext = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
            }

            // 生成随机文件名
            String randomFilename = UUID.randomUUID().toString().toLowerCase() + ext;
            Path savePath = dir.resolve(randomFilename);

            // 写入文件
            Files.copy(file.getInputStream(), savePath);

            log.info("文件已保存到: {}", savePath);
            return Result.success("上传成功", randomFilename);

        } catch (IOException e) {
            log.error("文件上传失败", e);
            return Result.error("文件保存失败: " + e.getMessage());
        }
    }

    /*//上传文件
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("文件为空");
        }

        try {
            // 获取 classpath 下 templates 的真实路径
            URL resource = AIController.class.getClassLoader().getResource("templates");
            if (resource == null) {
                return Result.error("templates 目录未找到，请确认 src/main/resources/templates 存在");
            }
            Path templatesDir = Paths.get(resource.toURI());

            // 获取扩展名
            String originalFilename = file.getOriginalFilename();
            String ext = "";
            if (originalFilename != null && originalFilename.contains(".")) {
                ext = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
            }

            // 生成随机文件名
            String randomFilename = UUID.randomUUID().toString().toLowerCase() + ext;
            Path savePath = templatesDir.resolve(randomFilename);

            // 写入文件
            Files.copy(file.getInputStream(), savePath);

            log.info("文件已保存到: {}", savePath);
            return Result.success("上传成功", randomFilename);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("文件保存失败: " + e.getMessage());
        }
    }*/

    //简历优化
    @PostMapping("/resume")
    public Result AIJL(@RequestBody AIParams params) throws IOException {
        log.info("开始简历优化{}",params);
        PageResult<AIResult> result = aiService.AIJL(params.getFile(),params.getUserPrompt());
        return Result.success("简历优化成功",result);
    }
}
